package cn.com.backstage.controller;

import java.io.File;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.*;
import cn.com.api.service.*;
import cn.com.backstage.controller.base.*;

import cn.com.util.ImageUtils;
import cn.com.util.*;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_BACKSTAGE + "/")
public class T_newsController extends T_newsBaseController {

	private static Logger logger = Logger.getLogger(T_newsController.class);

	@Autowired
	private ApiT_newsService apiT_newsService;
	@Autowired
	private StartOnLoadService startOnLoadService;
	
	/**
	 * 广告位进入查询界面
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_news_ad-list")
	public ModelAndView t_news_ad_list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(
				"forward:/backstage/t_news_ad_list.jsp");

		List<ApiT_news> t_newss = apiT_newsService.findT_newssByCtype("app");
		List<ApiT_news> t_newss2 = apiT_newsService
				.findT_newssByCtype("app_shops");
		t_newss.addAll(t_newss2);
		mv.addObject("t_newss", t_newss);

		//
		List<ApiT_city> citys = startOnLoadService.citys();
		mv.addObject("citys", citys);

		return mv;
	}

	/**
	 * 广告位进入新增或者编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_news_ad-add-pre")
	public ModelAndView t_news_ad_add_pre(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(
				"forward:/backstage/t_news_ad_add.jsp");
		//
		List<ApiT_city> citys = startOnLoadService.citys();
		mv.addObject("citys", citys);

		//
		if (request.getParameter("id") != null) {
			String id = request.getParameter("id");
			ApiT_news t_news = apiT_newsService.findT_newsById(Integer
					.valueOf(id));
			mv.addObject("t_news", t_news);
		}
		return mv;
	}

	/**
	 * 上传广告图
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "t_news_ad-img-upload")
	public void t_news_ad_img_upload(
			@RequestParam(value = "file", required = false) MultipartFile file,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// ModelMap model

		String path = ImageUtils.getServletWebAppsPath(request)+"/api/ads/";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		String sdfStr = sdf.format(new Date());
		String fileName = sdfStr + "_" + file.getOriginalFilename();

		File targetFile = new File(path, fileName);
		if (!targetFile.exists()) {
			targetFile.mkdirs();
		}
		String smallpic_path = "";
		Map<String, Object> map = new HashMap<String, Object>();
		// 保存
		try {
			file.transferTo(targetFile);
			smallpic_path = ImageUtils.getDataBaseImagePath()+"/api/ads/" + fileName;

			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "上传成功！");
			map.put("datas", smallpic_path);

		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
		}
		//
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 广告位新增或者编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_news_ad-add")
	public void t_news_ad_add(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String rid = request.getParameter("rid");
		String titles = request.getParameter("titles");
		String ctype = request.getParameter("ctype");
		String keyword = request.getParameter("keyword");
		String contents = request.getParameter("contents");
		String appdate = request.getParameter("appdate");
		String origin = request.getParameter("origin");
		String appip = request.getParameter("appip");
		String appuser = request.getParameter("appuser");
		String modtype = request.getParameter("modtype");
		String enable = request.getParameter("enable");
		String accept = request.getParameter("accept");
		String ctype_b = request.getParameter("ctype_b");
		String city_id = request.getParameter("city_id");
		String picname = request.getParameter("picname");
		String c_order = request.getParameter("c_order");
		String price = request.getParameter("price");
		String status = request.getParameter("status");
		String product_id = request.getParameter("product_id");
		String goods_id = request.getParameter("goods_id");

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");
		// add
		if (id == null || (id.trim()).equals("")) {
			ApiT_news t_news = new ApiT_news();

			t_news.setTitles(titles);
			t_news.setCtype(ctype);
			t_news.setKeyword(keyword);
			t_news.setContents(contents);
			t_news.setOrigin(origin);
			t_news.setEnable(Integer.valueOf(enable));
			t_news.setCity_id(Integer.valueOf(city_id));
			t_news.setC_order(Integer.valueOf(c_order));
			t_news.setPicname(picname);
			t_news.setGoods_id(goods_id);
			if(ctype_b!=null&&ctype_b!=""){
				t_news.setCtype_b(Integer.parseInt(ctype_b));
			}else{
				t_news.setCtype_b(null);
			}
			if(product_id!=null && product_id!=""){
				t_news.setProduct_id(Long.valueOf(product_id));
			}else{
				t_news.setProduct_id(0);
			}
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String str_now = sdf.format(new Date());
			t_news.setAppdate(str_now);
			/*
			 * t_news.setAppip(appip); t_news.setAppuser(appuser);
			 * t_news.setModtype(modtype); t_news.setAccept(accept);
			 * t_news.setCtype_b(ctype_b); t_news.setPrice(price);
			 * t_news.setStatus(status);
			 */

			apiT_newsService.insertT_news(t_news);
		}
		// edit
		else {
			ApiT_news t_news = apiT_newsService.findT_newsById(Integer
					.valueOf(id));

			t_news.setTitles(titles);
			t_news.setCtype(ctype);
			t_news.setKeyword(keyword);
			t_news.setContents(contents);
			t_news.setOrigin(origin);
			t_news.setEnable(Integer.valueOf(enable));
			t_news.setCity_id(Integer.valueOf(city_id));
			t_news.setC_order(Integer.valueOf(c_order));
			t_news.setPicname(picname);
			t_news.setGoods_id(goods_id);
			if(ctype_b!=null&&ctype_b!=""){
				t_news.setCtype_b(Integer.parseInt(ctype_b));
			}else{
				t_news.setCtype_b(null);
			}
			if(product_id!=null && product_id!=""){
				t_news.setProduct_id(Long.valueOf(product_id));
			}else{
				t_news.setProduct_id(0);
			}
			apiT_newsService.updateT_news(t_news);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 广告位 删除
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_news_ad-delete")
	public void t_news_ad_delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");

		// delete
		int ret_i = apiT_newsService.deleteT_news(Integer.valueOf(id));

		//
		if (ret_i > 0) {
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

}
